import pygal
import pymysql


def get_comments():
    # 连接数据库
    # 读表
    # select content from
    db = pymysql.connect(host='127.0.0.1',
                         port=3306,
                         user='root',
                         password='123456',
                         db='taobao',
                         charset='utf8mb4')
    with db.cursor() as cursor:
        sql = 'select leixing from taobao.table_name'
        cursor.execute(sql)
        s_c = cursor.fetchall()
        # print(s_c)
        list=[]
        for s in s_c:
            list.append(s[0])

        color = ','.join(list)
        print(color)
        color_index(color)

def color_index(color):
    s = color
    golden = s.count("金色")
    x = color
    black = x.count("黑色")
    z = color
    blue = z.count("蓝色")
    f = color
    white = f.count("白色")
    bing_tu(golden, black, blue,white)


def bing_tu(golden,black,blue,white):
    pie_chart = pygal.Pie()
    pie_chart.title = '购买ipone手机颜色的占比 2018 (in %)'
    pie_chart.add('金色', golden)
    pie_chart.add('黑色', black)
    pie_chart.add('蓝色', blue)
    pie_chart.add('白色', white)
    pie_chart.render_to_file('xiaomi.svg')

if __name__ == '__main__':
    get_comments()